package com.yztstudy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yztstudy.dto.Result;
import com.yztstudy.dto.UserDTO;
import com.yztstudy.entity.User;
import com.yztstudy.entity.UserInfo;
import com.yztstudy.mapper.UserInfoMapper;
import com.yztstudy.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yztstudy.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Resource
    private IUserService userService;

    /**
     * TODO 根据ID查询用户
     * @param userId 用户ID
     * @return
     */
    @Override
    public Result getUserById(Long userId) {

        // TODO 1.查询出用户
        User user = userService.getById(userId);

        // TODO 2.查询详情
        if (user == null)
        {
            return Result.ok();
        }
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);

        return Result.ok(userDTO);
    }


    /**
     * TODO 根据ID查询用户信息
     * @param userId 用户ID
     * @return
     */
    @Override
    public Result info(Long userId) {

        // TODO 1.查询详情
        UserInfo info = getById(userId);
        if (info == null) {
            // 没有详情，应该是第一次查看详情
            return Result.ok();
        }
        info.setCreateTime(null);
        info.setUpdateTime(null);

        // TODO 2.返回
        return Result.ok(info);
    }
}
